package life.majiang.community.controller;

import life.majiang.community.dto.CommentCreateDTO;
import life.majiang.community.dto.CommentDTO;
import life.majiang.community.dto.CommentLikeCountDTO;
import life.majiang.community.dto.ResultDTO;
import life.majiang.community.enums.CommentTypeEnum;
import life.majiang.community.exception.CustomizeErrorCode;
import life.majiang.community.exception.CustomizeException;
import life.majiang.community.model.Comment;
import life.majiang.community.model.User;
import life.majiang.community.service.CommentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by codedrinker on 2019/5/30.
 */
@Controller
public class CommentController {

    @Autowired
    private CommentService commentService;

    @ResponseBody
    @RequestMapping(value = "/comment", method = RequestMethod.POST)
    public Object post(@RequestBody CommentCreateDTO commentCreateDTO,
                       HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return ResultDTO.errorOf(CustomizeErrorCode.NO_LOGIN);
        }

        if (commentCreateDTO == null || StringUtils.isBlank(commentCreateDTO.getContent())) {
            return ResultDTO.errorOf(CustomizeErrorCode.CONTENT_IS_EMPTY);
        }

        Comment comment = new Comment();
        comment.setParentId(commentCreateDTO.getParentId());
        comment.setContent(commentCreateDTO.getContent());
        comment.setType(commentCreateDTO.getType());
        comment.setGmtModified(System.currentTimeMillis());
        comment.setGmtCreate(System.currentTimeMillis());
        comment.setCommentator(user.getId());
        comment.setLikeCount(0L);
        comment.setAnonymous(commentCreateDTO.getAnonymous());
        commentService.insert(comment, user);
        return ResultDTO.okOf();
    }

    @ResponseBody
    @RequestMapping(value = "/comment/{id}", method = RequestMethod.GET)
    public ResultDTO<List<CommentDTO>> comments(@PathVariable(name = "id") Long id) {
        List<CommentDTO> commentDTOS = commentService.listByTargetId(id, CommentTypeEnum.COMMENT);
        return ResultDTO.okOf(commentDTOS);
    }

    @ResponseBody
    @PostMapping(value = "/comment/like")
    public ResultDTO updateLikeCount(@RequestBody CommentLikeCountDTO commentLikeCount, HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if(user == null){
            return ResultDTO.errorOf(500, "未登录用户");
        }else{
            Comment comment = new Comment();
            comment.setId(commentLikeCount.getId());
            comment.setLikeCount(commentLikeCount.getLikeCount());
            commentLikeCount.setUserId(user.getId());

            commentService.updateLikeCount(comment, commentLikeCount);
            return ResultDTO.okOf();
        }
    }

//    删除评论
    @ResponseBody
    @DeleteMapping("/comment/{id}")
    public ResultDTO deleteCommentById(@PathVariable(name = "id") String id, @RequestParam("type") Integer type, @RequestParam("likeCount") Long likeCount, @RequestParam("commentCount") Integer commentCount,
                                       @RequestParam("parentId") Long parentId){
        Long commentId = null;
        boolean flag = true;
        try {
            commentId = Long.parseLong(id);
        } catch (NumberFormatException e) {
            throw new CustomizeException(CustomizeErrorCode.INVALID_INPUT);
        }
        if(type == 2){
            commentService.removeSecodeCommentByIdAndUpdateCommentCount(commentId, parentId);
        }else{
            flag = commentService.removeCommentByIdAndTypeEQ1(commentId, likeCount, commentCount, parentId);
        }

        if(!flag){
            throw new CustomizeException(CustomizeErrorCode.INVALID_INPUT);
        }else{
            return ResultDTO.okOf();
        }
    }
}
